package org.brownflat.lighthouse.util;


import org.apache.log4j.Logger;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

public class PropertyPlaceholder extends PropertyPlaceholderConfigurer {

	private static final Logger LOG = Logger.getLogger(PropertyPlaceholder.class);
    private Properties properties;

    private final static String HOME = System.getenv("LIGHTHOUSE_HOME");

    public PropertyPlaceholder() {
		try {
			loadPropertiesFromHome();
		} catch (Exception e) {
			LOG.error(e);
		}
    }

	protected String resolvePlaceholder(String placeholder, Properties props) {
        if(properties != null) {
    	    String value = properties.getProperty(placeholder);
	        LOG.info(String.format("Loading property [%s] with [%s].", placeholder, value));
		    return value;
        }
        return null;
	}

    private void loadPropertiesFromHome() {
        String home = System.getenv("LIGHTHOUSE_HOME");
        File file = new File(home + "/lighthouse.properties");
        try {
            FileReader fileReader = new FileReader(file);
            if(fileReader.ready() && file.exists()) {
                LOG.info("Loading default configuration properties");
                properties = new Properties();
                properties.load(fileReader);
            } else {
                loadDefaultProperties();
                LOG.warn("Creating default properties file");
            }
        } catch (IOException ioe) {
            try {
                loadDefaultProperties();
            } catch (IOException e) {
                LOG.fatal(e);
            }
        }
    }

    private void loadDefaultProperties() throws IOException {
        LOG.error(String.format("Lighthouse Configuration not found in LIGHTHOUSE_HOME [%s]", HOME));
        LOG.error("Properties file needs entries for:");
        LOG.error("server.host - the host name of the CI server");
        LOG.error("server.port - the port of the CI server");
        LOG.error("server.path - any path on the end of the url");
        LOG.error("active.server - the CI server to use (TeamCity, Hudson)");
        LOG.error("hudson.server.view - the view in hudson where the builds are found");
        LOG.error("hudson.build.failmatch - regex of patterns to look for failed hudson builds, eg (?i)(.*fail.*|.*unstable.*|.*failure.*)");
        LOG.error("teamcity.build.pattern - the regex pattern of TeamCity project or build name to match to");
        LOG.error("device.executable - the name of the build light executable, found in LIGHTHOUSE_HOME");
        LOG.error("Default properties loaded.");
        properties = new Properties();
        properties.put("server.host", "localhost");
        properties.put("server.port", "8080");
        properties.put("server.path", "");
        properties.put("active.server", "TeamCity");
        properties.put("hudson.server.views", "");
        properties.put("hudson.build.failmatch", "(?i)(.*fail.*|.*unstable.*|.*failure.*)");
        properties.put("teamcity.build.pattern", ".*");
        properties.put("device.executable", "USBCMDAP.exe");
        File propertiesFile = new File(HOME + "/lighthouse.properties");
        propertiesFile.createNewFile();
        FileOutputStream out = new FileOutputStream(propertiesFile);
        properties.store(out, "properties");
        out.flush();
    }
}
